package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-21 17:56
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.PetMapper;
import com.datang.pet.pojo.Pet;
import com.datang.pet.pojo.PetExample;
import com.datang.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl implements PetService {

    @Autowired
    private PetMapper petMapper;


    /*
     * @author 顶风少年
     * @date 2019/6/21 18:03
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param pet
     * @result void
     * @notify 添加宠物
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void addPet(Pet pet) throws Exception {
    
        pet.setId(StringUtils.getId());
        pet.setRegisterDate(new Date());
        try {
            petMapper.insert(pet);
        } catch (Exception e) {
            throw new Exception("添加宠物失败");
        }

    }
    /*
     * @author 顶风少年
     * @date 2019/6/21 18:03
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param pet
     * @result void
     * @notify 修改宠物信息
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatePet(Pet pet) throws Exception {
  
        try {
            petMapper.updateByPrimaryKeySelective(pet);
        } catch (Exception e) {
            throw new Exception("修改宠物信息失败");
        }

    }

    /*
     * @author 顶风少年
     * @date 2019/6/21 18:08
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result java.util.List<Pet>
     * @notify 获取用户的宠物
     * version 1.0
     */
    public List<Pet> getAllPetByUserId(String userId)throws  Exception{
        PetExample petExample = new PetExample();
        PetExample.Criteria criteria = petExample.createCriteria();
        criteria.andBelongUserIdEqualTo(userId);
        petExample.setOrderByClause("sequence asc");
        try {
          return  petMapper.selectByExample(petExample);
        }catch (Exception e){
            throw new Exception("获取宠物列表失败");
        }

    }
}
